package com.chao.wind3j.base.config.dozerConfig;

import com.chao.wind3j.base.parentClass.model.CastModel;
import com.github.pagehelper.Page;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CastBean {

  @Autowired
  private DozerBeanMapper dozerBeanMapper;

  public <T> T castModelToVo(CastModel obj, Class<T> target) {
    return this.dozerBeanMapper.map(obj, target);
  }

  public <T> List<T> castModelToVo(List<?> obj, Class<T> target) {
    var castObj = new Page<T>();
    if (obj instanceof Page) {
      BeanUtils.copyProperties(obj, castObj);
    } else {
      castObj.setPageNum(1);
      castObj.setPageSize(obj.size());
      castObj.setTotal(obj.size());
    }
    BeanUtils.copyProperties(obj, castObj);
    for (var item : obj) {
      castObj.add(this.dozerBeanMapper.map(item, target));
    }
    return castObj;
  }
}
